package com.koc.robot.util;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import com.koc.robot.GraphicsHelper;

/**
 * This class performs tasks on the side of OpenGL thread.
 */
public class GLResourceHelper 
{
	private static GLResourceHelper _sharedResourceHelper = new GLResourceHelper();
	
	/** singleton of the CCTouchDispatcher */
	public static GLResourceHelper sharedHelper()
	{
		return _sharedResourceHelper;
	}
	
	private ArrayList<WeakReference<Resource>> resourceList;
	
	public GLResourceHelper()
	{
		resourceList = new ArrayList<WeakReference<Resource>>();
	}
	
	public void AddGLResource(Resource resource)
	{
		resourceList.add(new WeakReference<Resource>(resource));
	}
	
	/*
	 * This should be called only when recreating GL context
	 */
	public void reloadResources()
	{
		GraphicsHelper.bindTexture(0);
		
		ArrayList<WeakReference<Resource>> remove = new ArrayList<WeakReference<Resource>>();
		for (WeakReference<Resource> item : resourceList)
		{
			Resource tempResource = item.get();
			if (tempResource == null)
			{
				remove.add(item);
			}
			else
			{
				tempResource.reload();
			}
		}
		
		resourceList.removeAll(remove);
	}
}
